import os
import re
import sys
import time
import webbrowser

import util_store
from logger import Logger

library_path = r'E:\PlayniteLibraryHtml'

url_skip_list = util_store.read_url_list("resume_breakpoint_url_list.txt")
sys.stdout = Logger()


def is_guid(basename):
    guid_pattern = re.compile(r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$')
    return guid_pattern.match(basename)


for dirpath, dirnames, filenames in os.walk(library_path):
    count = 0
    skip_count = 0
    for filename in filenames:
        basename, extension = os.path.splitext(filename)
        relative_path = dirpath.replace(library_path, "").lstrip("\\").lstrip("/")  # strip the leading path
        if filename.endswith(".html") and is_guid(basename):
            path = os.path.join(dirpath, filename).replace("\\", "/")
            if os.path.basename(path) not in url_skip_list:
                print(f"{count} opening {path}...")
                webbrowser.open(path)
                count += 1
                util_store.append_url("resume_breakpoint_url_list.txt", os.path.basename(path))
                time.sleep(10)
            else:
                skip_count += 1
                print(f"skipped {skip_count} {path}")
